/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javafxapplication;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.input.MouseEvent;

/**
 *
 * @author Administrator
 */
public class FXMLDocumentController implements Initializable {

    @FXML
    private ProgressBar progress;
    @FXML
    private Button progressButton;
    @FXML
    private Label label;

    @FXML
    private void handleButtonAction(ActionEvent event) {
        System.out.println("You clicked me!");
        label.setText("Hello World!");
    }

    @FXML
    private void itemAction1() {
        label.setText("Hello World! itemAction1 ");
    }

    @FXML
    private void itemAction2() {
        label.setText("Hello World! itemAction2");
    }

    @FXML
    private void closeAction(MouseEvent mouseEvent) {
        label.setText("Hello World! closeAction " + mouseEvent.getX());
        System.exit(0);

    }
    private Task copyWorker;

    @FXML
    private void progressAction(ActionEvent action) {
        label.setText("Hello World! progressAction ");
        progress.setProgress(0);
        progressButton.setDisable(false);
        copyWorker = createWorker();
        progress.progressProperty().unbind();
        progress.progressProperty().bind(copyWorker.progressProperty());
        copyWorker.messageProperty().addListener(new ChangeListener<String>() {
            public void changed(ObservableValue<? extends String> observable, String oldValue,
                    String newValue) {
                System.out.println(newValue);
            }
        });
        new Thread(copyWorker).start();
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }

    public Task createWorker() {
        return new Task() {
            @Override
            protected Object call() throws Exception {
                for (int i = 0; i < 10; i++) {
                    Thread.sleep(1000);
                    updateMessage("2000 milliseconds");
                    updateProgress(i + 1, 10); 
                }
                return true;
            }
        };
    }

}
